<?php
// +----------------------------------------------------------------------
// | 鸿运集团 [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

namespace app\admin\controller;

use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;
/**
 * 数据中心
 */

class Data extends Permissions
{
	//门店数据
    public function storelist()
    {
        $post = $this->request->param();
		$where = [];
		$where1 = [];
        if (isset($post['store_name']) and !empty($post['store_name'])) {
            $where['a.id'] = $post['store_name']; 
        }
        
        if(!empty(input('start_time')) && !empty(input('end_time'))){
			$where['b.add_time'] = array('egt',input('start_time'));
			$where1['b.add_time'] = array('elt',input('end_time'));
		}
		
		$where2['type'] = ['not in',[4,7]];
		$where2['pay_type'] = ['not in',[6]];
		$where2['state'] = ['eq',2]; 
		//查询订单总额/笔数，实收金额 /笔数
        $total = db('paylog')->alias('b')->where($where2)->where($where1)->group('store_id')->field('sum(origin_price)-sum(tk_money) as num,sum(origin_price)-sum(balance) as num1,sum(balance)-sum(tk_money) as num2,store_id,add_time')->buildSql();
		$total4 = db('paylog')->alias('b')->where('type',4)->where($where1)->group('store_id')->field('sum(tk_money) as num3,count(id) as num4,store_id')->buildSql();
		 
		$storelist = db('store')->alias('a') 
			->join("$total as b",'b.store_id = a.id','LEFT') 
			->join("$total4 as f",'f.store_id = a.id','LEFT')
			->where($where)
			->where($where1)
		    ->order("a.show_asc asc") 
			->field('a.*,b.num,b.num1,b.num2,f.num3,f.num4,b.add_time')
			->paginate(10,false,['query'=>request()->param()]);
	 
	 
		$this->assign('storelist',$storelist);
		$this->assign('start_time',input('start_time'));
		$this->assign('end_time',input('end_time'));
		$this->assign('store_id',input('store_name'));
		//查询出所有门店
		$store = Db::name('store')->select();
        $this->assign('store',$store);
		return $this->fetch();
       
    }
    //订单流水
	public function orderlist()
	{
        $post = $this->request->param();
        if (isset($post['store_name']) and !empty($post['store_name'])) {
            $where['b.id'] = $post['store_name'];
        }
        
        if (isset($post['group']) and !empty($post['group'])) {
            $where['d.id'] = $post['group'];
        }
        
        if (isset($post['name']) and !empty($post['name'])) {
            $where['c.id'] = $post['name'];
        }
        
        if (isset($post['state']) and !empty($post['state'])) {
            $where['a.state'] = $post['state'];
        }
        
        if (isset($post['pay_type']) and !empty($post['pay_type'])) {
            $where['a.pay_type'] = $post['pay_type'];
        }
        
       	if(!empty($post['start_time']) && !empty($post['end_time'])){ 
			$where['a.add_time'] = ['between time',[$post['start_time'],$post['end_time']]];
		} 
        $this->assign('end_time',input('end_time'));
		$this->assign('start_time',input('start_time'));
		
        $orderlist = empty($where) ? db('paylog')->alias('a')
        ->join('store b','a.store_id=b.id','LEFT')
        ->join('emp c','a.staff_id=c.id','LEFT')
        ->join('group d','a.group_id=d.id','LEFT')
        ->field('a.*,b.store_name,c.name,d.group')
        ->order('add_time desc')->paginate(10) : db('paylog')
        ->alias('a')->join('store b','a.store_id=b.id','LEFT')
        ->join('emp c','a.staff_id=c.id','LEFT')
        ->join('group d','a.group_id=d.id','LEFT')
        ->field('a.*,b.store_name,c.name,d.group')
        ->where($where)->order('add_time desc')->paginate(10,false,['query'=>$this->request->param()]);
        
        $this->assign('orderlist',$orderlist);
        $info['hyjt_store'] = Db::name('store')->select();
        $info['hyjt_group'] = Db::name('group')->select();
        $info['hyjt_cashier'] = Db::name('emp')->where('role_name_id',23)->select();
        $this->assign('info',$info);
        $this->assign('store_id',input('store_name'));
        $this->assign('group_id',input('group'));
        $this->assign('name_id',input('name'));
		$this->assign('end_time',input('end_time'));
		$this->assign('start_time',input('start_time'));
        return $this->fetch();
	}
	//收银员数据
	public function cashierlist()
	{
        $post = $this->request->param();
		$where = [];
		$where1 = [];
        if (isset($post['store_name']) and !empty($post['store_name'])) {
            $where['b.id'] = $post['store_name'];
        }
        
        if (isset($post['group']) and !empty($post['group'])) {
            $where['c.id'] = $post['group'];
        }
        
        if (isset($post['name']) and !empty($post['name'])) {
            $where['a.id'] = $post['name'];
        }
        
        if (isset($post['state']) and !empty($post['state'])) {
            $where['a.state'] = $post['state'];
        }
        
        if (isset($post['pay_type']) and !empty($post['pay_type'])) {
            $where['a.pay_type'] = $post['pay_type'];
        }
        
        if(!empty(input('start_time')) && !empty(input('end_time'))){
			$where['d.add_time'] = array('egt',input('start_time'));
			$where1['d.add_time'] = array('elt',input('end_time'));
		}
		$where2['type'] = ['not in',[4,7]];
		$where2['pay_type'] = ['not in',[6]];
		$where2['state'] = ['eq',2]; 
		//查询订单总额/笔数，实收金额 /笔数
        $total = db('paylog')->alias('d')->where($where2)->where($where1)->group('staff_id')->field('sum(origin_price)-sum(tk_money) as num,sum(origin_price)-sum(balance) as num1,sum(balance)-sum(tk_money) as num2,staff_id,add_time')->buildSql();
		$total3 = db('paylog')->alias('d')->where('type',4)->where($where1)->group('staff_id')->field('sum(tk_money) as num3,count(id) as num4,staff_id')->buildSql();
		
		$roleid = db('admin_cate')->where('name','收银员')->value('id');
        $cashierlist = db('emp')->where('role_name_id',$roleid)->alias('a')
        ->join('store b','a.store_name_id=b.id','LEFT')
        ->join('group c','a.group_name_id=c.id','LEFT')
        ->join("$total as d",'d.staff_id = a.id','LEFT') 
		->join("$total3 as g",'g.staff_id = a.id','LEFT') 
        ->field('a.*,b.store_name,c.group,d.num,d.num1,d.num2,g.num3,g.num4,d.add_time')
        ->where($where)->where($where1)->order('a.show_asc asc')->paginate(10,false,['query'=>$this->request->param()]);
        
        $this->assign('cashierlist',$cashierlist);
        $info['hyjt_store'] = Db::name('store')->select();
        $info['hyjt_group'] = Db::name('group')->select();
        $info['hyjt_cashier'] = Db::name('emp')->where('role_name_id',$roleid)->select();
        $this->assign('info',$info); 
        $this->assign('store_id',input('store_name'));
        $this->assign('group_id',input('group'));
        $this->assign('name_id',input('name'));
		$this->assign('end_time',input('end_time'));
		$this->assign('start_time',input('start_time'));
        return $this->fetch();
	}
    //佣金数据
	public function salesmanlist()
	{
		//会员开卡总金额
		$ywy_yj = db('commission')->where('type',1)->field('sum(balance) as yj,count(id) as num')->select(); 
		$this->assign('ywy_yj',$ywy_yj[0]); 
		//查询角色
		$cate = db('admin_cate')->field('name,id')->select();
		$this->assign('cate',$cate); 
		
        if(!empty(input('start_time')) && !empty(input('end_time'))){
			$where['c.add_time'] = ['between time',[input('start_time'),input('end_time')]];
		}
		if(!empty(input('user_name'))){
			$where['u.user_name'] = input('user_name');
		}  
		if(!empty(input('cate_id'))){
			$where['a.id'] = input('cate_id');
		}
		$type = input('type') ? input('type') : 1; 
		$where['c.type'] = $type; 
		 
		$list = db('commission')->alias('c')
		->join('user u','c.ywy_id = u.id','LEFT')
		->join('emp e','u.id = e.user_id','LEFT')
		->join('admin_cate a','a.id = e.role_name_id','LEFT')
		->field('u.user_name,a.id,a.name,a.commission,sum(c.balance) as je,count(c.id) num,sum(c.ywy_yj) as yj,c.ywy_id')
		->where($where)
		->group('c.ywy_id')
		->order('c.add_time desc')->paginate(10,false,['query'=>$this->request->param()]);
		$this->assign('list',$list); 
		$this->assign('cate_id',input('cate_id')); 
		$this->assign('user_name',input('user_name')); 
		$this->assign('start_time',input('start_time')); 
		$this->assign('end_time',input('end_time')); 
		$this->assign('type',$type); 
        return $this->fetch();
	}
	//门店数据详情
    public function storeinfo()
    {
    	$where['type'] = ['not in',[4,7]];
		$where['pay_type'] = ['not in',[6]];
		$where['state'] = ['eq',2]; 
		//查询订单总额/笔数，实收金额 /笔数，优惠金额
        $total = db('paylog')->alias('b')->where($where)->group('store_id')->field('sum(origin_price)-sum(tk_money) as num,count(id) as num1,sum(balance)-sum(tk_money) as num3,store_id,add_time')->buildSql();
		$total22 = db('paylog')->alias('b')->where('type',4)->group('store_id')->field('sum(tk_money) as num22,count(id) as num23,store_id')->buildSql();
		  
		//优惠金额笔数
		$total1 = db('paylog')->where($where)->where('origin_price <> balance')->group('store_id')->field('sum(origin_price)-sum(balance) as num2,count(id) as num4,store_id')->buildSql();
		
	    $total6 = db('paylog')->where('state',2)->where('pay_type',3)->group('store_id')->field('sum(balance) as num6,store_id')->buildSql();
		$total7 = db('paylog')->where('state',2)->where('pay_type',3)->group('store_id')->field('count(id) as num7,store_id')->buildSql();
	    $total8 = db('paylog')->where('state',2)->where('pay_type',2)->group('store_id')->field('sum(balance) as num8,store_id')->buildSql();
		$total9 = db('paylog')->where('state',2)->where('pay_type',2)->group('store_id')->field('count(id) as num9,store_id')->buildSql();
	    $total10 = db('paylog')->where('state',2)->where('pay_type',8)->group('store_id')->field('sum(balance) as num10,store_id')->buildSql();
		$total11 = db('paylog')->where('state',2)->where('pay_type',8)->group('store_id')->field('count(id) as num11,store_id')->buildSql();
	    $total12 = db('paylog')->where('state',2)->where('pay_type',1)->group('store_id')->field('sum(balance) as num12,store_id')->buildSql();
		$total13 = db('paylog')->where('state',2)->where('pay_type',1)->group('store_id')->field('count(id) as num13,store_id')->buildSql();
	    $total14 = db('consume')->where('type',1)->group('store_id')->field('sum(balance) as num14,store_id')->buildSql();
		$total15 = db('consume')->where('type',1)->group('store_id')->field('count(id) as num15,store_id')->buildSql();
		//查看会员消费
		$where2['type'] = ['in',[3,8,9]];
		$total16 = db('paylog')->where($where2)->where('pay_type',5)->group('store_id')->field('sum(balance) as num16,count(id) as num17,store_id')->buildSql();

	    $total18 = db('consume')->where('type',3)->group('store_id')->field('sum(num) as num18,store_id')->buildSql();
		$total19 = db('consume')->where('type',3)->group('store_id')->field('count(id) as num19,store_id')->buildSql();
	    $total20 = db('consume')->where('type',4)->group('store_id')->field('sum(num) as num20,store_id')->buildSql();
		$total21 = db('consume')->where('type',4)->group('store_id')->field('count(id) as num21,store_id')->buildSql(); 
		
		$info = db('store')->alias('a')
		    ->join("$total as b",'b.store_id = a.id','LEFT')
			->join("$total1 as c",'c.store_id = a.id','LEFT') 
			->join("$total6 as h",'h.store_id = a.id','LEFT')
			->join("$total7 as i",'i.store_id = a.id','LEFT')
			->join("$total8 as j",'j.store_id = a.id','LEFT')
			->join("$total9 as k",'k.store_id = a.id','LEFT')
			->join("$total10 as l",'l.store_id = a.id','LEFT')
			->join("$total11 as m",'m.store_id = a.id','LEFT')
			->join("$total12 as n",'n.store_id = a.id','LEFT')
			->join("$total13 as o",'o.store_id = a.id','LEFT')
			->join("$total14 as p",'p.store_id = a.id','LEFT')
			->join("$total15 as q",'q.store_id = a.id','LEFT')
			->join("$total16 as r",'r.store_id = a.id','LEFT') 
			->join("$total18 as t",'t.store_id = a.id','LEFT')
			->join("$total19 as u",'u.store_id = a.id','LEFT')
			->join("$total20 as v",'v.store_id = a.id','LEFT')
			->join("$total21 as w",'w.store_id = a.id','LEFT')
			->join("$total22 as x",'x.store_id = a.id','LEFT') 
			->field('a.*,b.num,b.num1,c.num2,b.num3,c.num4,h.num6,i.num7,j.num8,k.num9,l.num10,m.num11,n.num12,o.num13,p.num14,q.num15,r.num16,r.num17,t.num18,u.num19,v.num20,w.num21,x.num22,x.num23')
			->where('a.id',input('id'))->find();
		$this->assign('info',$info);
		return $this->fetch();
    }
    //订单流水详情
    public function orderinfo()
    {
		$info = db('paylog')->alias('a')
		    ->join("user b",'a.user_id = b.id','LEFT')
			->join("emp c",'a.staff_id = c.id','LEFT')
			->join("store d",'a.store_id = d.id','LEFT')
			->join("group e",'a.group_id = e.id','LEFT')
            ->field('a.*,b.user_name,c.name,d.store_name,e.group')
			->where('a.id',input('id'))->find();
		$this->assign('info',$info);
		return $this->fetch();
    }
    //收银员数据详情
    public function cashierinfo()
    {
    	$where['type'] = ['not in',[4,7]];
		$where['pay_type'] = ['not in',[6]];
		$where['state'] = ['eq',2]; 
		//查询订单总额/笔数，实收金额 /笔数
        $total = db('paylog')->where($where)->group('staff_id')->field('sum(origin_price)-sum(tk_money) as num,count(id) as num1,sum(balance)-sum(tk_money) as num3,staff_id,add_time')->buildSql();
		//优惠金额笔数，优惠金额
		$total1 = db('paylog')->where($where)->where('origin_price <> balance')->group('staff_id')->field('sum(origin_price)-sum(balance) as num2,count(id) as num4,staff_id')->buildSql();
		
	    $total6 = db('paylog')->where('state',2)->where('pay_type',3)->group('staff_id')->field('sum(balance) as num6,staff_id')->buildSql();
		$total7 = db('paylog')->where('state',2)->where('pay_type',3)->group('staff_id')->field('count(id) as num7,staff_id')->buildSql();
	    $total8 = db('paylog')->where('state',2)->where('pay_type',2)->group('staff_id')->field('sum(balance) as num8,staff_id')->buildSql();
		$total9 = db('paylog')->where('state',2)->where('pay_type',2)->group('staff_id')->field('count(id) as num9,staff_id')->buildSql();
	    $total10 = db('paylog')->where('state',2)->where('pay_type',8)->group('staff_id')->field('sum(balance) as num10,staff_id')->buildSql();
		$total11 = db('paylog')->where('state',2)->where('pay_type',8)->group('staff_id')->field('count(id) as num11,staff_id')->buildSql();
	    $total12 = db('paylog')->where('state',2)->where('pay_type',1)->group('staff_id')->field('sum(balance) as num12,staff_id')->buildSql();
		$total13 = db('paylog')->where('state',2)->where('pay_type',1)->group('staff_id')->field('count(id) as num13,staff_id')->buildSql();
	    $total14 = db('consume')->where('type',1)->group('staff_id')->field('sum(balance) as num14,staff_id')->buildSql();
		$total15 = db('consume')->where('type',1)->group('staff_id')->field('count(id) as num15,staff_id')->buildSql();
		//查看会员消费
		$where2['type'] = ['in',[3,8,9]];
		$total16 = db('paylog')->where($where2)->where('pay_type',5)->group('staff_id')->field('sum(balance) as num16,count(id) as num17,staff_id')->buildSql();
		
	    $total18 = db('consume')->where('type',3)->group('staff_id')->field('sum(num) as num18,staff_id')->buildSql();
		$total19 = db('consume')->where('type',3)->group('staff_id')->field('count(id) as num19,staff_id')->buildSql();
	    $total20 = db('consume')->where('type',4)->group('staff_id')->field('sum(num) as num20,staff_id')->buildSql();
		$total21 = db('consume')->where('type',4)->group('staff_id')->field('count(id) as num21,staff_id')->buildSql(); 
		//查看退款总金额，总笔数
        $total22 = db('paylog')->where('type',4)->group('staff_id')->field('count(id) as num23,sum(tk_money) as num22,staff_id')->buildSql();
		
		$roleid = db('admin_cate')->where('name','收银员')->value('id');
		$info = db('emp')->where('role_name_id',$roleid)->alias('a')
		    ->join("store b",'a.store_name_id = b.id','LEFT')
			->join("group c",'a.group_name_id = c.id','LEFT')
			->join("$total as d",'d.staff_id = a.id','LEFT')
			->join("$total1 as e",'e.staff_id = a.id','LEFT') 
		    ->join("$total6 as j",'j.staff_id = a.id','LEFT')
			->join("$total7 as k",'k.staff_id = a.id','LEFT')
			->join("$total8 as l",'l.staff_id = a.id','LEFT')
			->join("$total9 as m",'m.staff_id = a.id','LEFT')
			->join("$total10 as n",'n.staff_id = a.id','LEFT')
			->join("$total11 as o",'o.staff_id = a.id','LEFT')
			->join("$total12 as p",'p.staff_id = a.id','LEFT')
			->join("$total13 as q",'q.staff_id = a.id','LEFT')
		    ->join("$total14 as r",'r.staff_id = a.id','LEFT')
			->join("$total15 as s",'s.staff_id = a.id','LEFT')
			->join("$total16 as t",'t.staff_id = a.id','LEFT') 
			->join("$total18 as v",'v.staff_id = a.id','LEFT')
			->join("$total19 as w",'w.staff_id = a.id','LEFT')
			->join("$total20 as x",'x.staff_id = a.id','LEFT')
			->join("$total21 as y",'y.staff_id = a.id','LEFT')
			->join("$total22 as z",'z.staff_id = a.id','LEFT') 
		    ->field('a.*,b.store_name,c.group,d.num,d.num1,e.num2,d.num3,e.num4,j.num6,k.num7,l.num8,m.num9,n.num10,o.num11,p.num12,q.num13,r.num14,s.num15,t.num16,t.num17,v.num18,w.num19,x.num20,y.num21,z.num22,z.num23')
			->where('a.id',input('id'))->find();
		$this->assign('info',$info);
		return $this->fetch();
    }
    //佣金数据详情
    public function salesmaninfo()
    {
		//查询出推荐人信息
		$id = input('id');
		$where['c.type'] = 1;
		$where['c.ywy_id'] = $id;
		$info = db('commission')->alias('c')
		->join('user u','u.id = c.ywy_id','LEFT')
		->where($where)->field('sum(c.ywy_yj) as yj,sum(c.balance) as je,count(c.id) num,u.user_name')->select();
		$this->assign('info',$info[0]); 
		
        $list = db('user')->alias('u') 
		->join('userlevel s','u.level_id = s.id','LEFT') 
		->field('u.user_name,u.mobile,u.card,u.kaquan,u.balance as u_balance,u.id,s.name')
		->where('u.tj_emp',$id)->select();
		//查询出赠送数量,开卡金额
		foreach($list as $k=>$v){
			$list[$k]['balance'] = db('commission')->where('type',1)->where('user_id',$v['id'])->sum('balance');
			$list[$k]['zs_num'] = db('userhouse')->where('user_id',$v['id'])->sum('num');
		}
		$this->assign('list',$list);
		return $this->fetch();
    }
    
        //门店数据导出
    public function storelist_export()
	{
		$where = [];
		$where1 = [];
        if(!empty(input('store_name'))) {
            $where['a.id'] = input('store_name');
        }
        
        if(!empty(input('start_time')) && !empty(input('end_time'))){
			$where['b.add_time'] = array('egt',input('start_time'));
			$where1['b.add_time'] = array('elt',input('end_time'));
		} 

        $where2['type'] = ['not in',[4,7]];
		$where2['pay_type'] = ['not in',[6]];
		$where2['state'] = ['eq',2]; 
		//查询订单总额/笔数，实收金额 /笔数
        $total = db('paylog')->alias('b')->where($where2)->where($where1)->group('store_id')->field('sum(origin_price)-sum(tk_money) as num,sum(origin_price)-sum(balance) as num1,sum(balance)-sum(tk_money) as num2,store_id,add_time')->buildSql();
		$total4 = db('paylog')->alias('b')->where('type',4)->where($where1)->group('store_id')->field('sum(tk_money) as num3,count(id) as num4,store_id')->buildSql();
		 
		$res = db('store')->alias('a') 
			->join("$total as b",'b.store_id = a.id','LEFT') 
			->join("$total4 as f",'f.store_id = a.id','LEFT')
			->where($where)
			->where($where1)
		    ->order("a.show_asc asc") 
			->field('a.*,b.num,b.num1,b.num2,f.num3,f.num4,b.add_time')
			->select();
        
        $str ="门店名称,订单总金额,订单优惠金额,实收金额,退款金额/笔数"."\n";
		
        $str = iconv('utf-8','gbk',$str); 
        foreach($res as $k=>$row){  
			$store_name = iconv('utf-8','gbk',$row['store_name']);
			$num = iconv('utf-8','gbk',$row['num']);		
			$num1 = iconv('utf-8','gbk',$row['num1']);		
			$num2 = iconv('utf-8','gbk',$row['num2']);
			$num3 = iconv('utf-8','gbk',$row['num3']);
			$num4 = iconv('utf-8','gbk',$row['num4']);
			
			$str .= $store_name.",".$num.",".$num1.",".$num2.",".$num3."/".$num4.","."\n"; //用引文逗号分开 
			
        } 
		
		$time = date('Y-m-d');
        $filename ="门店数据报表$time.csv";   //设置文件名  
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit; 
	}
    
    //订单流水导出
    public function orderlist_export()
	{
		$where = [];
        if(!empty(input('store_name'))) {
            $where['b.id'] = input('store_name');
        }
        
        if(!empty(input('group'))) {
            $where['d.id'] = input('group');
        }
        
        if(!empty(input('name'))) {
            $where['c.id'] = input('name');
        }
        
        if(!empty(input('state'))) {
            $where['a.state'] = input('state');
        }
        
        if(!empty(input('pay_type'))) {
            $where['a.pay_type'] = input('pay_type');
        }
        
       	if(!empty(input('start_time')) && !empty(input('end_time'))){ 
			$where['a.add_time'] = ['between time',input('start_time'),input('end_time')];
		} 
        $this->assign('end_time',input('end_time'));
		$this->assign('start_time',input('start_time'));
		
        $res = db('paylog')->alias('a')->join('store b','a.store_id=b.id','LEFT')
        ->join('emp c','a.staff_id=c.id','LEFT')
        ->join('group d','a.group_id=d.id','LEFT')
        ->field('a.*,b.store_name,c.name,d.group')
        ->where($where)->order('add_time desc')->select();
        
        $str ="收款订单号,状态,订单金额,优惠金额,实收金额,收款方式,收款门店,收银员,收款时间"."\n";
		
        $str = iconv('utf-8','gbk',$str); 
        foreach($res as $k=>$row){  
			$order_sn = iconv('utf-8','gbk',$row['order_sn']); 
			$state = iconv('utf-8','gbk',$row['state']);
			$num = iconv('utf-8','gbk',$row['origin_price']);		
			$num1 = iconv('utf-8','gbk',$row['balance']);		
			$num2 = iconv('utf-8','gbk',$row['balance']);
		    $pay_type = iconv('utf-8','gbk',$row['pay_type']);		
			$store_name = iconv('utf-8','gbk',$row['store_name']);
		    $name = iconv('utf-8','gbk',$row['name']);	
		    $add_time = iconv('utf-8','gbk',$row['add_time']);		
			
			$str .= $order_sn.",".$state.",".$num.",".$num1.",".$num2.",".$pay_type.",".$store_name.",".$name.",".$add_time.","."\n"; //用引文逗号分开 
			
        } 
		
		$time = date('Y-m-d');
        $filename ="订单流水报表$time.csv";   //设置文件名  
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit; 
	}

    //收银员数据导出
    public function cashierlist_export()
	{ 
		$where = [];
		$where1 = [];
        if(!empty(input('store_name'))) {
            $where['b.id'] = input('store_name');
        }
        
        if(!empty(input('group'))) {
            $where['c.id'] = input('group');
        }
        
        if(!empty(input('name'))) {
            $where['a.id'] = input('name');
        }
        
        if (isset($post['state']) and !empty($post['state'])) {
            $where['a.state'] = $post['state'];
        }
        
       if (isset($post['pay_type']) and !empty($post['pay_type'])) {
           $where['a.pay_type'] = $post['pay_type'];
        }
        
        if(!empty(input('start_time')) && !empty(input('end_time'))){
			$where['d.add_time'] = array('egt',input('start_time'));
			$where1['d.add_time'] = array('elt',input('end_time'));
		}  
		$where2['type'] = ['not in',[4,7]];
		$where2['pay_type'] = ['not in',[6]];
		$where2['state'] = ['eq',2]; 
		//查询订单总额/笔数，实收金额 /笔数
        $total = db('paylog')->alias('d')->where($where2)->where($where1)->group('staff_id')->field('sum(origin_price)-sum(tk_money) as num,sum(origin_price)-sum(balance) as num1,sum(balance)-sum(tk_money) as num2,staff_id,add_time')->buildSql();
		$total3 = db('paylog')->alias('d')->where('type',4)->where($where1)->group('staff_id')->field('sum(tk_money) as num3,count(id) as num4,staff_id')->buildSql();
		
		
		$roleid = db('admin_cate')->where('name','收银员')->value('id');
        $res = db('emp')->where('role_name_id',$roleid)->alias('a')
        ->join('store b','a.store_name_id=b.id','LEFT')
        ->join('group c','a.group_name_id=c.id','LEFT')
        ->join("$total as d",'d.staff_id = a.id','LEFT') 
		->join("$total3 as g",'g.staff_id = a.id','LEFT') 
        ->field('a.*,b.store_name,c.group,d.num,d.num1,d.num2,g.num3,g.num4,d.add_time')
        ->where($where)->where($where1)->order('a.show_asc asc')->select();
        
        $str ="收银员名称,门店,分组,订单金额,优惠金额,实收金额,退款总金额,退款总笔数"."\n";
		
        $str = iconv('utf-8','gbk',$str); 
        foreach($res as $k=>$row){  
			$name = iconv('utf-8','gbk',$row['name']); 
			$store_name = iconv('utf-8','gbk',$row['store_name']);
			$group = iconv('utf-8','gbk',$row['group']);		
			$num = iconv('utf-8','gbk',$row['num']);		
			$num1 = iconv('utf-8','gbk',$row['num1']);
		    $num2 = iconv('utf-8','gbk',$row['num2']);		
			$num3 = iconv('utf-8','gbk',$row['num3']);
		    $num4 = iconv('utf-8','gbk',$row['num4']);		
			
			$str .= $name.",".$store_name.",".$group.",".$num.",".$num1.",".$num2.",".$num3.",".$num4.","."\n"; //用引文逗号分开 
			
        } 
		
		$time = date('Y-m-d');
        $filename ="收银员报表$time.csv";   //设置文件名  
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit; 
	}

    //佣金数据导出
    public function salesmanlist_export()
	{
        if(!empty(input('start_time')) && !empty(input('end_time'))){
			$where['c.add_time'] = ['between time',[input('start_time'),input('end_time')]];
		}
		if(!empty(input('user_name'))){
			$where['u.user_name'] = input('user_name');
		}  
		if(!empty(input('cate_id'))){
			$where['a.id'] = input('cate_id');
		}
		$type = input('type') ? input('type') : 1; 
		$where['c.type'] = $type; 
		
		$res = db('commission')->alias('c')
		->join('user u','c.ywy_id = u.id','LEFT')
		->join('emp e','u.id = e.user_id','LEFT')
		->join('admin_cate a','a.id = e.role_name_id','LEFT')
		->field('u.user_name,a.id,a.name,a.commission,sum(c.balance) as je,count(c.id) num,sum(c.ywy_yj) as yj,c.ywy_id')
		->where($where)
		->group('c.ywy_id')
		->order('c.add_time desc')->select();
		if($type == 1){
			$str ="推荐人名称,角色,开卡金额,开卡笔数,提成比例,提成金额"."\n";
		}else{
			$str ="推荐人名称,角色,提现金额,提现笔数,提成比例"."\n";
		}
		
        $str = iconv('utf-8','gbk',$str); 
        foreach($res as $k=>$row){  
			$user_name = iconv('utf-8','gbk',$row['user_name']); 
			$name = iconv('utf-8','gbk',$row['name']);
			$je = iconv('utf-8','gbk',$row['je']);		
			$num = iconv('utf-8','gbk',$row['num']);		
			$commission = iconv('utf-8','gbk',$row['commission']);
			$yj = iconv('utf-8','gbk',$row['yj']);	 
			if($type == 1){
				$str .= $user_name.",".$name.",".$je.",".$num.",".$commission.'%'.",".$yj.","."\n"; //用引文逗号分开 
			}else{
				$str .= $user_name.",".$name.",".$je.",".$num.",".$commission.'%'.","."\n"; //用引文逗号分开 
			} 
        } 
		
		$time = date('Y-m-d');
        $filename ="佣金报表$time.csv";   //设置文件名  
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit; 
	}
}
